<?php
namespace api\task\controller;
use api\common\model\RlHiseServiceProjectModel;
use api\common\model\RlZsmsUserQueryModel;
use think\Controller;
use think\Log;
use think\Db;
use api\common\service\MsgService;
use api\common\model\RlApplyDelayModel;
ini_set('max_execution_time', 6000);
class ZdyxController extends Controller
{
    /*
     * 更改到自动运行的单子
     * */
        public function Automatic(){
        $azd_arr = [];
        $count=RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->count();
        $page=ceil($count/200);
        for($i=0;$i<=$page;$i++){
            $azd_list = RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->limit($i*200,200)->select()->toArray();
            foreach ($azd_list as $v) {
                $yqsqzq = RlApplyDelayModel::where(['status' => 1, 'azd_id' => $v['azd_id']])->sum('yqsqzq');
                $begintime = strtotime(date('Y-m-d 00:00:00', time()));
                $endtime = strtotime(date('Y-m-d 23:59:59', time()));
                $time = 9 + $yqsqzq;
                $yqtime = strtotime('+' . $time . ' month', $v['ycgcwcsj']);
                if ($yqtime >= $begintime && $yqtime <= $endtime) {
                    $azdId = $v['azd_id'];
                    $result=Db::name('message_log')->where('azd_id', $azdId)->find();
                    $msg_send = new MsgService();
                    $jxs = RlZsmsUserQueryModel::Where('bm_code',$v['jxs'])->where('gw',0)->where('status',1)->select()->toArray();
                    if (empty($jxs)) {
                        $datas['status']=0;
                        $datas['update_time'] = date("Y-m-d H:i:s", time());
                        $datas['info'] = '经销商不存在';
                        if($result){
                            Db::name('message_log')->where('azd_id', $azdId)->update($datas);
                        }else{
                            $datas['azd_id']=$v['azd_id'];
                            Db::name('message_log')->insert($datas);
                        }
                    }else{
                        foreach ($jxs as $key => $value) {
                            $phone = $value['mobilphone'];
                            $msgres = $msg_send->installsend8($azdId,$phone);
                            if($msgres){
                                $dataone['update_time'] = date("Y-m-d H:i:s", time());
                                $dataone['status'] = 1;
                                $dataone['info'] = '发送成功';
                                if($result){
                                    Db::name('message_log')->where('azd_id', $azdId)->update($dataone);
                                }else{
                                    $dataone['azd_id']=$v['azd_id'];
                                    Db::name('message_log')->insert($dataone);
                                }
                            }else{
                                $dataones['update_time'] = date("Y-m-d H:i:s", time());
                                $dataones['status'] = 0;
                                $dataones['info'] = '发送失败';
                                if($result){
                                    Db::name('message_log')->where('azd_id', $azdId)->update($dataones);
                                }else{
                                    $dataone['azd_id']=$v['azd_id'];
                                    Db::name('message_log')->insert($dataones);
                                }
                            }
                        }
                    }
                    $data['zdyxbs']=1;
                    $data['gczt'] = 5;
                    $data['update_time'] = time();
                    $data['bx_kssj'] = time();
                    $data['tssj'] = time();
                    $data['wgsj'] = time();
                    $data['tjrq'] = time();
                    $row = RlHiseServiceProjectModel::Where('azd_id',$v['azd_id'])->update($data);
                }
            }
        }
    }
    /*
     * 查询当天所有应该转运行的安装单
     * */
    public function AutomaticLog(){
        $azd_arr = [];
        $count=RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->count();
        $page=ceil($count/200);
        for($i=0;$i<=$page;$i++){
            $azd_list = RlHiseServiceProjectModel::where('gczt', 'in', '3,4')->where('ycgcsfjsbs', 1)->limit($i*200,200)->select()->toArray();
            foreach ($azd_list as $v) {
                $yqsqzq = RlApplyDelayModel::where(['status' => 1, 'azd_id' => $v['azd_id']])->sum('yqsqzq');
                $begintime = strtotime(date('Y-m-d 00:00:00', time()));
                $endtime = strtotime(date('Y-m-d 23:59:59', time()));
                $time = 9 + $yqsqzq;
                $yqtime = strtotime('+' . $time . ' month', $v['ycgcwcsj']);
                if ($yqtime >= $begintime && $yqtime <= $endtime){
                      $azd_arr['azd_id'] = $v['azd_id'];
                      $azd_arr['create_time'] = date("Y-m-d H:i:s", time());
                      $azd_arr['status'] = 0;
                      $azd_arr['type'] = 2;
                      Db::name('message_log')->insert($azd_arr);
                }
            }
        }
    }
    public function Automaticbeifen(){
        $azd_limit_id = 0;
        $i=0;
        while (1)
        {
            $azd_arr = [];
            $sms = [];
            $sms2 = [];
            $azd_list = RlHiseServiceProjectModel::where('gczt','in','3,4')->where('ycgcsfjsbs',1)->limit($i*200,200)->select()->toArray();
            if(empty($azd_list))
            {
                break;
            }
            foreach ($azd_list as $v){
                $azd_limit_id = $v['hise_service_project_id'];
                $azd_id= $v['azd_id'];
                $yqsqzq= RlApplyDelayModel::where(['status' => 1,'azd_id' =>$azd_id])->sum('yqsqzq');
                $nine=strtotime("-9 month");//9个月之前的时间戳   2021-6-29         2022-3-29   12
                if($nine >=$v['ycgcwcsj']){
                    if($v['is_apply_delay']){
                        if(strtotime('-' . $yqsqzq . ' month',$nine)>=$v['ycgcwcsj']){
                            $azd_arr[] = $v['azd_id'];
                            $sms[] = $v;
                        }
                        continue;
                    }else{
                        $azd_arr[] = $v['azd_id'];
                        $sms[] = $v;
                    }
                }
            }
            if(!empty($azd_arr)){
                $data = [
                    'zdyxbs'=>1,
                ];
                $data['gczt'] = 5;
                $data['update_time'] = time();
                $data['bx_kssj'] = time();
                $data['wgsj'] = time();
                $azd_id = implode(',',$azd_arr);
                $row = RlHiseServiceProjectModel::Where('azd_id','in',$azd_id)->update($data);
                if($row){
                    Log::write('自动转运行成功'.date('Y-h-d h:i',time()));
                }else{
                    Log::write('自动转运行失败'.date('Y-h-d h:i',time()));
                }
            }else{
                Log::write('暂时没有自动转运行的安装单'.date('Y-h-d h:i',time()));
            }
            $i++;
        }
    }
}

